package com.fzy.common.file.pdf.component;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * 设置页面附加属性
 */
public class PDFBuilder extends PdfPageEventHelper {

	private static Logger logger = LoggerFactory.getLogger(PDFBuilder.class);

	// 基础字体对象
	private BaseFont bf = getBaseFont();

	// 中文字体对象
	//private Font font = new XMLWorkerFontProvider().getFont("宋体", 12);

	//文档字体大小
	private int fontSize = 12;

	//模板
	private PdfTemplate template;

	//数据实体
	private Object data;

	//页眉页脚定制接口
	private IHeaderFooter iHeaderFooter;

	public PDFBuilder(IHeaderFooter iHeaderFooter) {
		this(iHeaderFooter, null);
	}

	public PDFBuilder(IHeaderFooter iHeaderFooter, Object data) {
		this(iHeaderFooter, data, 12);
	}

	public PDFBuilder(IHeaderFooter iHeaderFooter, Object data, int fontSize) {
		this.iHeaderFooter = iHeaderFooter;
		this.data = data;
		this.fontSize = fontSize;
	}

	/**
	 * @Desc 打开文档时，创建一个总页数的模版
	 * @author 冯赵杨
	 * @date 2017年5月8日 下午6:46:46
	 */
	@Override
	public void onOpenDocument(PdfWriter writer, Document document) {
		template = writer.getDirectContent().createTemplate(50, 50);
	}

	/**
	 * 关闭每页的时候，写入页眉,页脚等
	 */
	@Override
	public void onEndPage(PdfWriter writer, Document document) {
		try {
			this.addPage(writer, document);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @Desc 加分页
	 * @author 冯赵杨
	 * @date 2017年5月8日 下午6:46:01
	 */
	private void addPage(PdfWriter writer, Document document) throws Exception {
		if (iHeaderFooter != null) {
			//1.写入页眉
			iHeaderFooter.writeHeader(writer, document, data, new Font(bf, 12), template);
			//2.写入前半部分页脚
			iHeaderFooter.writeFooter(writer, document, data, new Font(bf, 12), template);
		}
	}

	/**
	 * 关闭文档时，替换模板，完成整个页眉页脚组件
	 */
	@Override
	public void onCloseDocument(PdfWriter writer, Document document) {
		if (iHeaderFooter != null) {
			template.beginText();
			template.setFontAndSize(bf, fontSize);
			String replace = iHeaderFooter.getReplaceOfTemplate(writer, document, data);
			template.showText(replace);
			template.endText();
			template.closePath();
		}
		/*String text = "总" + (writer.getPageNumber() - 1) + "页";
		ColumnText.showTextAligned(template, Element.ALIGN_LEFT, new Paragraph(text, new Font(bf, 12)), 2, 1, 0);*/
	}

	public int getPresentFontSize() {
		return fontSize;
	}

	public void setPresentFontSize(int fontSize) {
		this.fontSize = fontSize;
	}

	/**
	 * @Desc 获取基础中文字体
	 * @author 冯赵杨
	 * @date 2017年5月8日 下午5:12:00
	 */
	protected static BaseFont getBaseFont() {
		BaseFont bf = null;
		try {
			bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bf;
	}

	/**
	 * @Desc 设置字体-返回中文字体
	 * @param fontSize 字体大小，isBold 是否加粗
	 * @author 冯赵杨
	 * @date 2017年5月3日 上午10:06:42
	 */
	protected static Font getChineseFont(int fontSize, boolean isBold) {
		BaseFont bfChinese;
		Font fontChinese = null;
		try {
			bfChinese = getBaseFont();
			if (isBold) {
				fontChinese = new Font(bfChinese, fontSize, Font.BOLD);
			} else {
				fontChinese = new Font(bfChinese, fontSize, Font.NORMAL);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fontChinese;
	}
}